package com.wjb.xietong.server.project;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.wjb.xietong.server.BaseResponseDataParse;
import com.wjb.xietong.server.HeaderInfoResponse;
import com.wjb.xietong.util.IDs;
import com.wjb.xietong.util.LogD;

public class ProjectResponse extends BaseResponseDataParse {
	private static ProjectResponse instance = null;

	private HeaderInfoResponse headerInfo = null;
	private int countNotify = 0;
	private List<ProjectInfo> projectInfoList = null;

	private ProjectResponse() {

	}

	public static synchronized ProjectResponse instance() {
		if (null == instance) {
			instance = new ProjectResponse();
		}
		return instance;
	}

	@Override
	public void initData() {
		setHeaderInfo(null);
		setCountNotify(0);
		setProjectInfoList(null);
	}

	@Override
	public boolean parseJsonObj(JSONObject response) {
		initData();

		if (null == response) {
			LogD.output("Exception: response is null");
			return false;
		}

		if (!checkRespCode(response)) {
			headerInfo = new HeaderInfoResponse();
			headerInfo.parseJsonObj(response);
			setHeaderInfo(headerInfo);
			return false;
		}

		if (response.has(IDs.COUNT_NOTIFY)) {
			setCountNotify(response.optInt(IDs.COUNT_NOTIFY));
		}

		if (response.has(IDs.WORK_SYS_PROJECT_DO_MAP_LIST)) {
			JSONArray jsonArray = response
					.optJSONArray(IDs.WORK_SYS_PROJECT_DO_MAP_LIST);
			if (null == jsonArray) {
				LogD.output("Warning: Data is null");
				return true;
			}

			projectInfoList = new ArrayList<ProjectResponse.ProjectInfo>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObj = jsonArray.optJSONObject(i);
				if (null == jsonObj) {
					continue;
				}

				ProjectInfo projectInfo = new ProjectInfo();
				if (jsonObj.has(IDs.TITLE)) {
					projectInfo.setTitle(jsonObj.optString(IDs.TITLE));
				}
				if (jsonObj.has(IDs.PROJECT_DESCRIPTION)) {
					projectInfo.setDescription(jsonObj
							.optString(IDs.PROJECT_DESCRIPTION));
				}
				if (jsonObj.has(IDs.PROJECT_ID)) {
					projectInfo.setProjectId(jsonObj.optLong(IDs.PROJECT_ID));
				}
				projectInfoList.add(projectInfo);
			}
			setProjectInfoList(projectInfoList);
		}
		return true;
	}

	public static class ProjectInfo {
		private String title = null;
		private String description = null;
		private long projectId = 0l;

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		public long getProjectId() {
			return projectId;
		}

		public void setProjectId(long projectId) {
			this.projectId = projectId;
		}

	}

	public HeaderInfoResponse getHeaderInfo() {
		return headerInfo;
	}

	public void setHeaderInfo(HeaderInfoResponse headerInfo) {
		this.headerInfo = headerInfo;
	}

	public int getCountNotify() {
		return countNotify;
	}

	public void setCountNotify(int countNotify) {
		this.countNotify = countNotify;
	}

	public List<ProjectInfo> getProjectInfoList() {
		return projectInfoList;
	}

	public void setProjectInfoList(List<ProjectInfo> projectInfoList) {
		this.projectInfoList = projectInfoList;
	}

}
